import sklearn.datasets as datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.metrics import precision_recall_curve
import matplotlib.pyplot as plt
import numpy as np

# print(datasets.load_digits())
x = datasets.load_digits().data.copy()
y = datasets.load_digits().target.copy()

y[y != 9] = 0
y[y == 9] = 1

x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=666)

log_reg = LogisticRegression()
log_reg.fit(x_train, y_train)
y_predict = log_reg.predict(x_test)

matrix = confusion_matrix(y_test, y_predict)

print(matrix)
# 准确率（预测正确/(预测正确+预测错误)）
print(precision_score(y_test, y_predict))
# 召回率（预测正确/(预测正确+未预测到)）
print(recall_score(y_test, y_predict))

#调和率
print(f1_score(y_test, y_predict))
# 评分方法，可以用于更改决策边界
# print(log_reg.decision_function(x_test))

# 绘制准确率和召回率曲线
precision, recall, thresholds = precision_recall_curve(y_test, log_reg.decision_function(x_test))

plt.plot(thresholds, precision[:-1])
plt.plot(thresholds, recall[:-1])
plt.show()

# ROC
from sklearn.metrics import roc_auc_score
print(roc_auc_score(y_test, log_reg.decision_function(x_test)))